<template>
  <Card dis-hover :bordered="false">
    <Row>
      <Col span="22">
        <Form inline :label-width="0" @submit.native.prevent>
          <FormItem>
            <Input
              placeholder="请输入平台名称"
              v-model="params.keywords"
              @on-clear="searchData"
              @on-enter="searchData"
              clearable
            />
          </FormItem>
          <FormItem>
            <Select
              v-width="120"
              placeholder="内容类型"
              v-model="params.platform_content_type"
              @on-change="searchData"
              clearable
            >
              <Option
                v-for="item in contenttypelist"
                :value="item.value"
                :key="item.value"
                >{{ item.label }}</Option
              >
            </Select>
          </FormItem>
          <FormItem>
            <Select
              v-width="120"
              placeholder="请选择状态"
              v-model="params.status"
              @on-change="searchData"
              clearable
            >
              <Option
                v-for="item in statelist"
                :value="item.value"
                :key="item.value"
                >{{ item.label }}</Option
              >
            </Select>
          </FormItem>
        </Form>
      </Col>
      <Col span="2" style="text-align: right">
        <Button
          type="primary"
          @click="handleAdd"
          class="ivu-ml-8"
          v-hasC="{
            name: 'v1-buss-platform-update-platform',
            flag: 'v1/buss/platform/update-platform',
          }"
          >新增</Button
        >
      </Col>
    </Row>
    <Table
      :columns="columns"
      :data="list"
      :loading="loading"
      @on-sort-change="sortChange"
      :max-height="tableHeigth"
    >
      <template #status_name="{ row }">
        <Badge
          status="success"
          :text="row.status_name"
          v-if="row.status_name === '正常'"
        />
        <Badge status="error" :text="row.status_name" v-else />
      </template>
      <template #action="{ row }">
        <a
          @click="handleEdit(row)"
          v-hasC="{
            name: 'v1-buss-platform-update-platform2',
            flag: 'v1/buss/platform/update-platform',
          }"
          >编辑</a
        >
        <Divider
          type="vertical"
          v-hasC="{
            name: 'v1-buss-platform-update-platform2',
            flag: 'v1/buss/platform/update-platform',
          }"
        />
        <a
          @click="handleDel(row.id)"
          class="del"
          v-hasC="{
            name: 'v1-buss-platform-delete-platform',
            flag: 'v1/buss/platform/delete-platform',
          }"
          >删除</a
        >
      </template>
    </Table>
    <Page
      class="ivu-mt-16"
      :total="total"
      :modelValue="params.current"
      :page-size="params.size"
      :page-size-opts="pageOpts"
      show-total
      show-sizer
      @on-change="changePage"
      @on-page-size-change="changepageSize"
    />
    <Modal
      :mask-closable="false"
      v-model="addModal"
      :title="edit ? '新增' : '编辑'"
      :footer-hide="true"
      :draggable="true"
    >
      <Form ref="create" :model="platInfo" :rules="rules" :label-width="80">
        <FormItem label="名称" prop="name" required>
          <Input
            v-model="platInfo.name"
            placeholder="请输入名称"
            :maxlength="30"
          />
        </FormItem>

        <FormItem label="内容类型" prop="platform_content_type" required>
          <CheckboxGroup v-model="platInfo.platform_content_type">
            <template v-for="item in contenttypelist" :key="item.value">
              <Checkbox :label="item.value"
                ><span>{{ item.label }}</span></Checkbox
              >
            </template>
          </CheckboxGroup>
        </FormItem>
        <FormItem label="认证规则" prop="authenticator_rules">
          <Input
            v-model="platInfo.authenticator_rules"
            placeholder="请输入认证规则"
            type="textarea"
            :rows="6"
          />
        </FormItem>
        <FormItem label="状态" prop="status">
          <Switch v-model="platInfo.status" />
        </FormItem>
      </Form>
      <div slot="footer" style="text-align: right">
        <Button type="default" @click="addModal = false">取消</Button>
        <Button
          type="primary"
          :loading="btnLoading"
          @click="ok"
          style="margin-left: 10px"
          >确定</Button
        >
      </div>
    </Modal>
  </Card>
</template>
<script>
import { platformList, createPlatform, deletePlatform } from "@api/platform.js";
export default {
  name: "platform",
  data() {
    return {
      tableHeigth: document.body.clientHeight - 280,
      addModal: false,
      edit: true,
      params: {
        keywords: "",
        platform_content_type: "",
        status: "",
        current: 1,
        size: 10,
        order_column: "",
        order_sort: "",
      },
      contenttypelist: [],
      statelist: [],
      total: 0,
      pageOpts: [10, 20, 30, 40],
      columns: [
        {
          key: "id",
          title: "ID",
          width: 80,
        },
        {
          key: "name",
          title: "名称",
          ellipsis: "true",
          tooltip: "true",
          width: 140,
        },
        {
          key: "account_num",
          title: "账号数",
          width: 100,
          sortable: "custom",
        },
        {
          key: "platform_content_type_str",
          title: "内容类型",
          width: 175,
        },
        {
          key: "authenticator_rules",
          title: "认证规则",
          ellipsis: "true",
          tooltip: "true",
          minWidth: 140,
        },
        {
          slot: "status_name",
          title: "状态",
          width: 80,
        },
        {
          slot: "action",
          title: "操作",
          width: 110,
          fixed: "right",
        },
      ],
      list: [],
      loading: false,
      btnLoading: false,
      platInfo: {
        id: "",
        name: "",
        platform_content_type: [],
        authenticator_rules: "",
        status: true,
      },
      rules: {
        name: [
          {
            required: true,
            message: "平台名称长度不得超过30",
            pattern: /^.{1,30}$/,
          },
        ],
        platform_content_type: [
          {
            required: true,
            message: "请选择内容类型",
            type: "array",
            min: 1,
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    sortChange(e) {
      this.params.order_column = e.key.replace(/_str$/, "");
      this.params.order_sort = e.order;
      this.getData();
    },
    //切换page
    changePage(index) {
      this.params.current = index;
      this.getData();
    },
    changepageSize(val) {
      this.params.size = val;
      this.getData();
    },
    handleDel(index) {
      this.$Modal.confirm({
        title: "删除提示",
        content: "是否确认删除此条记录，删除后不可恢复",
        onOk: () => {
          deletePlatform({ id: index })
            .then(() => {
              this.$Message.success("删除成功");
              this.getData();
            })
            .catch(() => {});
        },
      });
    },
    handleAdd() {
      this.platInfo = {
        id: "",
        name: "",
        platform_content_type: [],
        authenticator_rules: "",
        status: true,
      };
      this.addModal = true;
      this.edit = true;
      this.$refs.create.resetFields();
    },
    handleEdit(info) {
      this.addModal = true;
      this.edit = false;
      this.platInfo = {
        id: info.id,
        name: info.name,
        platform_content_type: info.platform_content_type,
        authenticator_rules: info.authenticator_rules,
        status: info.status == 1 ? true : false,
      };
    },
    getData() {
      this.loading = true;
      const params = {
        keywords: this.params.keywords,
        platform_content_type: this.params.platform_content_type,
        order_column: this.params.order_column,
        order_sort: this.params.order_sort,
        status: this.params.status,
        pageSize: this.params.size,
        page: this.params.current,
      };
      platformList(params)
        .then((res) => {
          this.list = res.list;
          this.contenttypelist = res.condition.type_arr;
          this.statelist = res.condition.status_arr;
          this.total = res.total;
          this.loading = false;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    searchData() {
      this.params.current = 1;
      this.getData();
    },
    addPatient() {
      this.btnLoading = true;
      const params = {
        name: this.platInfo.name,
        platform_content_type: this.platInfo.platform_content_type.join(","),
        authenticator_rules: this.platInfo.authenticator_rules,
        status: this.platInfo.status ? 1 : 0,
      };
      if (this.edit) {
        createPlatform(params)
          .then((res) => {
            this.$Message.success("录入成功");
            this.addModal = false;
            this.btnLoading = false;
            this.params.current = 1;
            this.getData();
          })
          .catch(() => {
            this.btnLoading = false;
          });
      } else {
        createPlatform(Object.assign({}, params, { id: this.platInfo.id }))
          .then((res) => {
            this.$Message.success("编辑成功");
            this.addModal = false;
            this.btnLoading = false;
            this.getData();
          })
          .catch(() => {
            this.btnLoading = false;
          });
      }
    },
    ok() {
      this.$refs.create.validate((valid) => {
        if (valid) {
          this.addPatient();
        } else {
          this.addModal = true;
          // this.$Message.error('填写错误，保存失败！')
          return;
        }
      });
    },
  },
};
</script>
